package com.egdigital.allzic.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnActionExpandListener;
import com.egdigital.allzic.R;
import com.egdigital.allzic.ui.ScrollingTextView;
import com.egdigital.allzic.ui.adapter.AdapterTabs;
import com.egdigital.allzic.ui.fragment.FragmentTabAllzik;
import com.egdigital.allzic.ui.fragment.FragmentTabCategory;
import com.egdigital.allzic.ui.fragment.FragmentTabExplore;
import com.egdigital.allzic.ui.fragment.FragmentTabFavorite;

import fr.nomeo.AppsPanel;

public class ActivityMain extends BaseActivity implements OnClickListener {

	private static final String	TAG				= ActivityMain.class.getSimpleName();	// log
	public static final int		INDEX_ALLZIC	= 0;
	public static final int		INDEX_CATEGORY	= 1;
	public static final int		INDEX_EXPLORE	= 2;
	public static final int		INDEX_FAVORITE	= 3;
	private EditText			mSearchEdit;
	public ViewPager			mPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		AppsPanel.getInstance().onCreate(this);
		
		setContentView(R.layout.activity_main); // layout
		// push appspanel
		// abs
		ActionBar mActionBar;
		mActionBar = getSupportActionBar();
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		mActionBar.setTitle(getString(R.string.app_name));
		mActionBar.setHomeButtonEnabled(true);
		// pager
		mPager = (ViewPager) findViewById(R.id.main_pager);
		mPager.setOffscreenPageLimit(5);
		//
		mPlayerLayout = (LinearLayout) findViewById(R.id.player_layout);
		mPlayerButtonPlay = (ImageButton) findViewById(R.id.player_play);
		mPlayerButtonFavorite = (ImageButton) findViewById(R.id.player_like);
		mPlayerSubTitle = (ScrollingTextView) findViewById(R.id.player_sub);
		mPlayerTitle = (TextView) findViewById(R.id.player_title);
		mPlayerThumb = (ImageView) findViewById(R.id.player_thumb);
		mPlayerLoading = (ProgressBar) findViewById(R.id.player_loading);
		//
		mPlayerButtonPlay.setEnabled(false);
		mPlayerButtonPlay.setOnClickListener(this);
		mPlayerLayout.setOnClickListener(this);
		mPlayerButtonFavorite.setOnClickListener(this);
		// tabs
		AdapterTabs tabsAdatper = new AdapterTabs(this, mPager);
		tabsAdatper.addTab(mActionBar.newTab().setText(getString(R.string.activity_main_allzik)), FragmentTabAllzik.class, null);
		tabsAdatper.addTab(mActionBar.newTab().setText(getString(R.string.activity_main_category)), FragmentTabCategory.class, null);
		tabsAdatper.addTab(mActionBar.newTab().setText(getString(R.string.activity_main_explore)), FragmentTabExplore.class, null);
		tabsAdatper.addTab(mActionBar.newTab().setText(getString(R.string.activity_main_favorite)), FragmentTabFavorite.class, null);
		try {
			mPager.setCurrentItem(getIntent().getExtras().getInt(ActivityHome.EXTRA_SHORTCUT)); // extra
		} catch (Exception e) {
		}
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onRestart() {
		AppsPanel.getInstance().onRestart();
		super.onRestart();
	}

	@Override
	protected void onStop() {
		AppsPanel.getInstance().onStop();
		super.onStop();
	}
	
	@Override
	protected void onResume() {
		// hide the keyboard trick
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View view) {
		super.onClick(view); // parent onClick()
	}

	@Override
	public void onBackPressed() {
		switch (mPager.getCurrentItem()) {
			case INDEX_ALLZIC:
				super.onBackPressed();
				overridePendingTransition(R.anim.slide_from_behind, R.anim.slide_out_right);
				break;
			case INDEX_CATEGORY:
				try {
					if (!removeFragment()) {
						super.onBackPressed();
						overridePendingTransition(R.anim.slide_from_behind, R.anim.slide_out_right);
					}
				} catch (Exception e) {
				}
				break;
			case INDEX_EXPLORE:
				try {
					if (!removeFragment()) {
						super.onBackPressed();
						overridePendingTransition(R.anim.slide_from_behind, R.anim.slide_out_right);
					}
				} catch (Exception e) {
				}
				break;
			case INDEX_FAVORITE:
				super.onBackPressed();
				overridePendingTransition(R.anim.slide_from_behind, R.anim.slide_out_right);
				break;
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		getSupportMenuInflater().inflate(R.menu.menu_main, menu);
		mSearchEdit = (EditText) menu.findItem(R.id.action_search).getActionView();
		// search menuitem
		final MenuItem searchItem = menu.findItem(R.id.action_search);
		searchItem.setOnActionExpandListener(new OnActionExpandListener() {

			@Override
			public boolean onMenuItemActionCollapse(MenuItem item) {
				// Do something when collapsed
				closeKeyboardFromFocus();
				return true; // Return true to collapse action view
			}

			@Override
			public boolean onMenuItemActionExpand(MenuItem item) {
				if (mSearchEdit.requestFocus()) {
					getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
				}
				openKeyboard();
				return true; // Return true to expand action view
			}
		});
		mSearchEdit.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					moveToSearchActivity(v.getText().toString(), true);
					return true;
				}
				return false;
			}
		});
		return super.onPrepareOptionsMenu(menu);
	}

	/**
	 * clean fragment BackStack
	 * 
	 * @return true if there was no error, else false
	 */
	public boolean removeFragment() {
		return getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
}
